package com.example.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.common.to.ware.SkuHasStockTo;
import com.example.common.to.ware.LockStockResultTo;
import com.example.common.to.ware.WareSkuLockTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.ware.entity.WareSkuEntity;
import com.example.gulimall.ware.service.WareSkuService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 商品库存
 *
 * @author zyred
 * @email zyred@163.com
 * @date 2021-08-23 11:58:31
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);
        return R.ok().put("page", page);
    }

//    @PostMapping("/lock/ware")
//    public List<LockStockResultTo> lockWare (@RequestBody WareSkuLockTo to) {
//        return this.wareSkuService.lockWare(to);
//    }
    @PostMapping("/lock/ware")
    public R lockWare (@RequestBody WareSkuLockTo to) {
        try {
            this.wareSkuService.lockWare(to);
            return R.ok();
        } catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @PostMapping("/hasStock")
    public List<SkuHasStockTo> hasStock(@RequestBody List<Long> skuIds){
        return wareSkuService.hasStock(skuIds);
    }

    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);
        return R.ok().put("wareSku", wareSku);
    }

    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);
        return R.ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
